package com.totoro.tyrank.listener;

import com.totoro.tyrank.constants.InvRankTypeConst;
import com.totoro.tyrank.constants.NBTKeyConst;
import com.totoro.tyrank.inventory.*;
import com.totoro.tyrank.utils.NBTUtil;
import com.totoro.tyrank.utils.StrUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class RankContentInventoryListener implements Listener {

    @EventHandler
    public void OnFire(InventoryClickEvent e) {
        Player player = (Player) e.getView().getPlayer();
        if(! RankContentInventory.currentOpenRankContent.containsKey(player.getUniqueId())) {
            return;
        }
        String rankContentInvTitle = RankContentInventory.currentOpenRankContent.get(player.getUniqueId());
        if(e.getView().getTitle().equals(rankContentInvTitle)) {
            e.setCancelled(true);
            // 获取点击物品
            ItemStack currentItem = e.getCurrentItem();
            if(null == currentItem || currentItem.getType() == Material.AIR || currentItem.getType() == Material.STAINED_GLASS_PANE) {
                return;
            }
            // 获取NBT上之前的RankType类型
            // 获取NBT上的rankId
            String rankId = "";
            String fromInvRankType = NBTUtil.getNBT(currentItem, NBTKeyConst.FROM_INV_RANK_TYPE);
            if(!StrUtil.isNotBlank(fromInvRankType)) {
                fromInvRankType = InvRankTypeConst.ALL_RANK_INV;
            }
            rankId = NBTUtil.getNBT(currentItem, NBTKeyConst.RANK_ID);
            // 获取点击类型
            ClickType clickType = e.getClick();
            // 如果点击的是属性
            if(e.getRawSlot() == 47) {
                AttributeReceiveInventory.openMainInv(player, rankId, fromInvRankType);
            }

            // 如果点击的是返回
            if(e.getRawSlot() == 49) {
                RankContentInventory.currentOpenRankContent.remove(player.getUniqueId());
                RankContentInventory.currentPage.put(player.getUniqueId(), 1);
                if(fromInvRankType.equals(InvRankTypeConst.ALL_RANK_INV)) {
                    AllRankInventory.openMainInv(player);
                }
                if(fromInvRankType.equals(InvRankTypeConst.CURRENT_RANK_INV)) {
                    CurrentRankInventory.openMainInv(player);
                }
                if(fromInvRankType.equals(InvRankTypeConst.HISTORY_RANK_INV)) {
                    HistoryRankInventory.openMainInv(player);
                }
            }

            // 如果点击的是奖励
            if(e.getRawSlot() == 51) {
                RewardReceiveInventory.openMainInv(player, rankId, fromInvRankType);
            }

            // 如果点击的是上一页，判断左键还是右键
            if(e.getRawSlot() == 45) {
                RankContentInventory.currentChangePageStatus.put(player.getUniqueId(), true);
                if(clickType == ClickType.LEFT) {
                    RankContentInventory.preCurrentPage(player, rankId, fromInvRankType);
                } else if (clickType == ClickType.RIGHT) {
                    RankContentInventory.firstCurrentPage(player, rankId, fromInvRankType);
                }
                RankContentInventory.currentChangePageStatus.put(player.getUniqueId(), false);
                return;
            }
            // 如果点击的是下一页，判断左键还是右键
            if(e.getRawSlot() == 53) {
                RankContentInventory.currentChangePageStatus.put(player.getUniqueId(), true);
                if(clickType == ClickType.LEFT) {
                    RankContentInventory.nextCurrentPage(player, rankId, fromInvRankType);
                } else if (clickType == ClickType.RIGHT) {
                    RankContentInventory.lastCurrentPage(player, rankId, fromInvRankType);
                }
                RankContentInventory.currentChangePageStatus.put(player.getUniqueId(), false);
                return;
            }
        }
    }

    @EventHandler
    public void onClose(InventoryCloseEvent e) {
        Player player = (Player) e.getView().getPlayer();
        if(! RankContentInventory.currentOpenRankContent.containsKey(player.getUniqueId())) {
            return;
        }
        String rankContentInvTitle = RankContentInventory.currentOpenRankContent.get(player.getUniqueId());
        Boolean changeStatus = RankContentInventory.currentChangePageStatus.getOrDefault(player.getUniqueId(), false);
        if(e.getView().getTitle().equals(rankContentInvTitle)) {
            if(! changeStatus) {
                RankContentInventory.currentPage.put(player.getUniqueId(), 1);
            }
        }
    }

}
